﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EnvironmentalMonitor.Support.Hibernate.Manage;
using EnvironmentalMonitor.Support.Hibernate;
using EnvironmentalMonitor.Support.Module.Manage;
using EnvironmentalMonitor.Support.Module;

namespace EnvironmentalMonitor.Support.Business.Manage
{
    /// <summary>
    /// 用户业务
    /// </summary>
    public class UserBusiness
    {
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页行数</param>
        /// <param name="total">总数</param>
        /// <returns>用户集合</returns>
        public List<User> Query(int page, int rows, ref int total)
        {
            List<User> results = new List<User>();

            UserHibernate hibernate = new UserHibernate();
            results = hibernate.Query(page, rows, ref total);

            return results;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="account">帐号</param>
        /// <param name="password">密码</param>
        /// <returns>用户</returns>
        public User Login(string account, string password)
        {
            User result = null;

            UserHibernate hibernate = new UserHibernate();
            User user = hibernate.SelectByAccount(account);
            if (user != null)
            {
                if ((user.Password == password) && (user.Validity))
                {
                    result = user;
                    user.Authentication = true;
                }
            }
            else
            {
                int count = hibernate.Count();
                if (count == 0)
                {
                    result = new User();
                    result.Guid = "Prerogative";
                    result.Prerogative = true;
                    result.Authentication = true;
                }
            }
            if ((result != null) && (result.Authentication))
            {
                PageHibernate pageHibernate = new PageHibernate();
                result.Pages = pageHibernate.QueryByUser(result.Guid);
                MenuHibernate menuHibernate = new MenuHibernate();
                result.Menus = menuHibernate.QueryByUser(result.Guid);
            }

            return result;
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Insert(User value)
        {
            bool result = false;

            UserHibernate hibernate = new UserHibernate();
            result = hibernate.Insert(value);

            return result;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Update(User value)
        {
            bool result = false;

            UserHibernate hibernate = new UserHibernate();
            result = hibernate.Update(value);

            return result;
        }

        /// <summary>
        /// 验证页面
        /// </summary>
        /// <param name="user">用户</param>
        /// <param name="pageName">页面类型</param>
        /// <returns>结果</returns>
        public bool VerifyPage(User user, Type pageType)
        {
            bool result = false;

            if (!result)
            {
                if (user.Prerogative)
                {
                    result = user.Prerogative;
                }
            }
            if (!result)
            {
                if (user.Pages != null)
                {
                    List<string> pageTypeNames = new List<string>();
                    while (pageType.BaseType != typeof(object))
                    {
                        pageTypeNames.Add(pageType.FullName);
                        pageType = pageType.BaseType;
                    }

                    for (int i = 0; i < user.Pages.Count; i++)
                    {
                        for (int j = 0; j < pageTypeNames.Count; j++)
                        {
                            if (user.Pages[i].Name == pageTypeNames[j])
                            {
                                result = true;
                                break;
                            }
                        }
                        if (result)
                        {
                            break;
                        }
                    }
                }
            }

            return result;
        }

        public bool Delete(User value)
        {
            bool result = false;

            UserHibernate hibernate = new UserHibernate();
            result = hibernate.Delete(value);

            return result;
        }

        public bool ChangePassword(string guid, string password)
        {
            bool result = false;

            UserHibernate hibernate = new UserHibernate();
            result = hibernate.ChangePassword(guid, password);

            return result;
        }
    }
}
